package com.qfedu.controller;

import com.qfedu.dto.DeveloperDto;
import com.qfedu.model.Developer;
import com.qfedu.page.PageResult;
import com.qfedu.page.QueryPageBean;
import com.qfedu.page.Result;
import com.qfedu.service.IDeveloperService;
import com.qfedu.vo.DevProVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 概述:
 *
 * @Author: Tbh
 * @Date: 2022/6/28 17:24
 */

@RestController
@RequestMapping("developer")
public class DeveloperController {

    @Autowired
    private IDeveloperService developerService;

//    @GetMapping("findAll")
//    public List<Developer> findAll() {
//        return developerService.findAll();
//    }

    @PostMapping("all")
    public PageResult all(@RequestBody QueryPageBean queryPage) {
        PageResult page = developerService.findPage(queryPage);
        return page;
    }

    @PostMapping("add")
    public Result addDeveloper(String dname,int[] pids) {
//    public Result addDeveloper(@RequestBody DevProVO devProVO) {
        int row = developerService.addAndBind(dname, pids);
        System.out.println(row);
        if (row > 0) {
            //枚举类名.对象名.get方法 获取到对应的属性值
            //Status.SUCCESS.name();  获取到枚举对象名  SUCCESS
            return Result.success();
        } else {
            return Result.fail();
        }
    }



    @GetMapping("findByDid")
    public Result findByDid(@RequestParam("did") String did) {
        DeveloperDto developerDto = developerService.findByDid(did);
        Result result = new Result();
        result.setMessage("fail");
        if (ObjectUtils.isNotEmpty(developerDto)) {
            result.setFlag(true);
            result.setData(developerDto);
            result.setMessage("success");
        }
        return result;
    }

    @PostMapping("edit")
public Result editDeveloper(String did,String dname,int[] pids) {
    int row = developerService.updateAndBind(did,dname, pids);
    System.out.println(row);
    if (row > 0) {
        //枚举类名.对象名.get方法 获取到对应的属性值
        //Status.SUCCESS.name();  获取到枚举对象名  SUCCESS
        return Result.success();
    } else {
        return Result.fail();
    }
}


    @GetMapping("delete")
    public Result delete(@RequestParam("did") String did) {
        int row = developerService.deleteByDid(did);
        if (row > 0) {
            return Result.success();
        } else {
            return Result.fail();
        }
    }
}
